package com.openbravo.possync;

import com.openbravo.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.data.loader.SentenceExec;
import com.openbravo.pos.customers.CustomerInfoExt;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.DataLogicSales;
import com.openbravo.pos.forms.DataLogicSystem;
import com.openbravo.pos.forms.ProcessAction;
import com.openbravo.pos.inventory.MovementReason;
import com.openbravo.pos.inventory.TaxCategoryInfo;
import com.openbravo.pos.ticket.CategoryInfo;
import com.openbravo.pos.ticket.ProductInfoExt;
import com.openbravo.pos.ticket.TaxInfo;
import com.openbravo.ws.customers.Customer;
import com.openbravo.ws.externalsales.Category;
import com.openbravo.ws.externalsales.Product;
import com.openbravo.ws.externalsales.ProductPlus;
import com.openbravo.ws.externalsales.Tax;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.Date;
import java.util.UUID;
import javax.xml.rpc.ServiceException;

public class ProductsSync
  implements ProcessAction
{
  private DataLogicSystem dlsystem;
  private DataLogicIntegration dlintegration;
  private DataLogicSales dlsales;
  private String warehouse;
  private ExternalSalesHelper externalsales;

  public ProductsSync(DataLogicSystem paramDataLogicSystem, DataLogicIntegration paramDataLogicIntegration, DataLogicSales paramDataLogicSales, String paramString)
  {
    this.dlsystem = paramDataLogicSystem;
    this.dlintegration = paramDataLogicIntegration;
    this.dlsales = paramDataLogicSales;
    this.warehouse = paramString;
    this.externalsales = null;
  }

  public MessageInf execute()
    throws BasicException
  {
    try
    {
      if (this.externalsales == null)
        this.externalsales = new ExternalSalesHelper(this.dlsystem);
      Product[] arrayOfProduct1 = this.externalsales.getProductsCatalog();
      Customer[] arrayOfCustomer = this.externalsales.getCustomers();
      if ((arrayOfProduct1 == null) || (arrayOfCustomer == null))
        throw new BasicException(AppLocal.getIntString("message.returnnull"));
      Object localObject1;
   //   Object localObject3;
      if (arrayOfProduct1.length > 0)
      {
        this.dlintegration.syncProductsBefore();
        localObject1 = new Date();
        for (Object localObject3 : arrayOfProduct1)
        {
          TaxCategoryInfo localTaxCategoryInfo = new TaxCategoryInfo(((Product)localObject3).getTax().getId(), ((Product)localObject3).getTax().getName());
          this.dlintegration.syncTaxCategory(localTaxCategoryInfo);
          TaxInfo localTaxInfo = new TaxInfo(((Product)localObject3).getTax().getId(), ((Product)localObject3).getTax().getName(), localTaxCategoryInfo.getID(), new Date(-9223372036854775808L), null, null, ((Product)localObject3).getTax().getPercentage() / 100.0D, false, Integer.valueOf(0));
          this.dlintegration.syncTax(localTaxInfo);
          CategoryInfo localCategoryInfo = new CategoryInfo(((Product)localObject3).getCategory().getId(), ((Product)localObject3).getCategory().getName(), null);
          this.dlintegration.syncCategory(localCategoryInfo);
          ProductInfoExt localProductInfoExt = new ProductInfoExt();
          localProductInfoExt.setID(((Product)localObject3).getId());
          localProductInfoExt.setReference(((Product)localObject3).getId());
          localProductInfoExt.setCode((((Product)localObject3).getEan() == null) || (((Product)localObject3).getEan().equals("")) ? ((Product)localObject3).getId() : ((Product)localObject3).getEan());
          localProductInfoExt.setName(((Product)localObject3).getName());
          localProductInfoExt.setCom(false);
          localProductInfoExt.setScale(false);
          localProductInfoExt.setPriceBuy(((Product)localObject3).getPurchasePrice());
          localProductInfoExt.setPriceSell(((Product)localObject3).getListPrice());
          localProductInfoExt.setCategoryID(localCategoryInfo.getID());
          localProductInfoExt.setTaxCategoryID(localTaxCategoryInfo.getID());
          localProductInfoExt.setImage(ImageUtils.readImage(((Product)localObject3).getImageUrl()));
          this.dlintegration.syncProduct(localProductInfoExt);
          if (!(localObject3 instanceof ProductPlus))
            continue;
          ProductPlus localProductPlus = (ProductPlus)localObject3;
          double d = localProductPlus.getQtyonhand() - this.dlsales.findProductStock(this.warehouse, localProductInfoExt.getID(), null);
          Object[] arrayOfObject = new Object[7];
          arrayOfObject[0] = UUID.randomUUID().toString();
          arrayOfObject[1] = localObject1;
          arrayOfObject[2] = (d > 0.0D ? MovementReason.IN_MOVEMENT.getKey() : MovementReason.OUT_MOVEMENT.getKey());
          arrayOfObject[3] = this.warehouse;
          arrayOfObject[4] = localProductInfoExt.getID();
          arrayOfObject[5] = new Double(d);
          arrayOfObject[6] = new Double(localProductInfoExt.getPriceBuy());
          this.dlsales.getStockDiaryInsert().exec(arrayOfObject);
        }
      }
      if (arrayOfCustomer.length > 0)
      {
        this.dlintegration.syncCustomersBefore();
        for (Customer localObject2 : arrayOfCustomer)
        {
          CustomerInfoExt localObject3 = new CustomerInfoExt(localObject2.getId());
          ((CustomerInfoExt)localObject3).setSearchkey(localObject2.getSearchKey());
          ((CustomerInfoExt)localObject3).setName(localObject2.getName());
          ((CustomerInfoExt)localObject3).setNotes(localObject2.getDescription());
          this.dlintegration.syncCustomer((CustomerInfoExt)localObject3);
        }
      }
      if ((arrayOfProduct1.length == 0) && (arrayOfCustomer.length == 0))
        return new MessageInf(-67108864, AppLocal.getIntString("message.zeroproducts"));
      return new MessageInf(-83886080, AppLocal.getIntString("message.syncproductsok"), AppLocal.getIntString("message.syncproductsinfo", new Object[] { Integer.valueOf(arrayOfProduct1.length), Integer.valueOf(arrayOfCustomer.length) }));
    }
    catch (ServiceException localServiceException)
    {
      throw new BasicException(AppLocal.getIntString("message.serviceexception"), localServiceException);
    }
    catch (RemoteException localRemoteException)
    {
      throw new BasicException(AppLocal.getIntString("message.remoteexception"), localRemoteException);
    }
    catch (MalformedURLException localMalformedURLException)
    {
    	   throw new BasicException(AppLocal.getIntString("message.malformedurlexception"), localMalformedURLException);

    }
   }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.possync.ProductsSync
 * JD-Core Version:    0.6.0
 */